using System;
using System.Collections.Generic;
using Castle.Windsor;
using Microsoft.Practices.ServiceLocation;

namespace MessagingWPF.Infrastructure
{
	public class WindsorAdapter : IServiceLocator
	{
		private readonly IWindsorContainer _Container;

		public WindsorAdapter(IWindsorContainer container)
		{
			_Container = container;
		}

		public object GetService(Type serviceType)
		{
			return _Container.GetService(serviceType);
		}

		public object GetInstance(Type serviceType)
		{
			return _Container.Resolve(serviceType);
		}

		public object GetInstance(Type serviceType, string key)
		{
			return _Container.Resolve(key, serviceType);
		}

		public IEnumerable<object> GetAllInstances(Type serviceType)
		{
			var list = new List<object>();
			var resolvedInstances = _Container.ResolveAll(serviceType);

			foreach (var resolvedInstance in resolvedInstances)
			{
				list.Add(resolvedInstance);
			}
			return list;
		}

		public TService GetInstance<TService>()
		{
			return _Container.Resolve<TService>();
		}

		public TService GetInstance<TService>(string key)
		{
			return _Container.Resolve<TService>(key);
		}

		public IEnumerable<TService> GetAllInstances<TService>()
		{
			return _Container.ResolveAll<TService>();
		}
	}
}